package com.tang.studentinformationmanagementsystem.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tang.studentinformationmanagementsystem.mapper.StuMapper;
import com.tang.studentinformationmanagementsystem.pojo.PageResult;
import com.tang.studentinformationmanagementsystem.pojo.StuQueryParam;
import com.tang.studentinformationmanagementsystem.pojo.Student;
import com.tang.studentinformationmanagementsystem.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private StuMapper stuMapper;

    //分页查询
    @Override
    public PageResult page(StuQueryParam stuQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(stuQueryParam.getPage(), stuQueryParam.getPageSize());

        //2.执行查询
        List<Student> list=stuMapper.pageFindAll(stuQueryParam);
        Page<Student> p = (Page<Student>) list;

        //3.封装结果
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        stuMapper.deleteByIds(ids);
    }

    @Override
    public void save(Student student) {
        if(student.getId()==null){
            student.setCreateTime(LocalDateTime.now());
            student.setUpdateTime(LocalDateTime.now());
            stuMapper.insert(student);
        }else {
            student.setUpdateTime(LocalDateTime.now());
            stuMapper.update(student);
        }
    }

    @Override
    public Student getById(Integer id) {
        Student student = stuMapper.getById(id);
        return student;
    }
}
